<template>
  <div class="home-container">
    <!-- 导航栏 -->
     <van-nav-bar class="page-nav-bar" fixed>
       <van-button
       slot="title"
       type="info"
       size="small"
       round
       icon="search"
       class="search-btn"
       >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
   <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <ArticleList :channel="channel"></ArticleList>
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
  <!-- /频道列表 -->

  <!-- 频道编辑弹出层 -->
  <van-popup
  v-model="isChennelEditShow"
  closeable
  position="bottom"
  :style="{ height: '100%' }"
  close-icon-position="top-left"
>
<ChannelEdit @update-active="onUpdateActive" :active="active" :my-channels="channels"></ChannelEdit>
  </van-popup>
  <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannel } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'homeItem',
  data () {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false // 控制编辑弹出层
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      let channels = []
      try {
        // const { data } = await getUserChannel()
        // this.channels = data.data.channels
        // console.log(data)
        // 判断当前用户有没有登录
        if (this.user) {
          const { data } = await getUserChannel()
          channels = data.data.channels
        } else {
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            // 如果缓存里面还是没有的话，就去请求默认的推荐列表
            const { data } = await getUserChannel()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道数据失败')
      }
    },
    onUpdateActive (index, isChennelEditShow) { // 子组件传递给父组件的值，更改点击频道进行切换
      // 更新激活的频道项
      this.active = index
      // 关闭编辑频道的弹窗
      this.isChennelEditShow = isChennelEditShow
    }
  }
}
</script>

<style scope lang='less'>
.home-container {
  padding-top: 174px;
   .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
  .van-icon {
    font-size: 32px;
    }
  }
  .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      width: 32px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      //  &表示当前元素的父元素
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(../../assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
